<template>
    <div class="table">
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item>
                    <i class="el-icon-menu"></i>{{$route.name}}
                </el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="handle-box">
            <el-dialog title="新增角色" :visible.sync="dialogFormVisible">
                <el-form :model="addForm" size="small">
                    <el-form-item label="角色名" :label-width="formLabelWidth">
                        <el-input v-model="addForm.name" auto-complete="off"></el-input>
                    </el-form-item>
                    <el-form-item label="备注" :label-width="formLabelWidth">
                        <el-input v-model="addForm.note" auto-complete="off"></el-input>
                    </el-form-item>
                </el-form>
                <div slot="footer" class="dialog-footer">
                    <el-button @click="dialogFormVisible = false">取 消</el-button>
                    <el-button type="primary" @click="add">确 定</el-button>
                </div>
            </el-dialog>
            <el-dialog title="修改角色" :visible.sync="dialogFormUpdate">
                <el-form :model="upForm" size="small">
                    <el-form-item label="角色名" :label-width="formLabelWidth">
                        <el-input v-model="upForm.name" auto-complete="off"></el-input>
                    </el-form-item>
                    <el-form-item label="备注" :label-width="formLabelWidth">
                        <el-input v-model="upForm.note" auto-complete="off"></el-input>
                    </el-form-item>
                </el-form>
                <div slot="footer" class="dialog-footer">
                    <el-button @click="dialogFormUpdate = false">取 消</el-button>
                    <el-button type="primary" @click="putRole">确 定</el-button>
                </div>
            </el-dialog>
            <el-input v-model="select_word" placeholder="输入角色名" class="search"></el-input>
            <el-button type="primary" icon="search" @click="search">搜索</el-button>
            <el-button type="primary" @click="dialogFormVisible = true">新增角色</el-button>
        </div>
        <div>
            <el-table :data="tableData" border fit style="width: 100%">
                <el-table-column prop="name" label="角色">
                </el-table-column>
                <el-table-column prop="note" label="备注">
                </el-table-column>
                <el-table-column fixed="right" label="操作">
                    <template slot-scope="scope">
                        <el-button @click="edit(scope.row)" type="text" size="small">编辑</el-button>
                        <el-button type="text" size="small" @click="delRole(scope.row)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
        </div>
        <div class="block">
            <el-pagination @current-change="handleCurrentChange" :current-page="page.number+1" layout="total, prev, pager, next, jumper" :total="page.totalElements">
            </el-pagination>
        </div>
    </div>
</template>
<script>
import { allRoles, delRole, putRole, addRole } from "../../server/getDate.js";
import { mapState, mapActions } from 'vuex'
import { getStore } from '../../utils/local.js'
import { confirm } from '../../utils/local'
export default {
    data() {
        return {
            value: '',
            loading: false,
            dialogTableVisible: false,
            dialogFormVisible: false,
            dialogFormUpdate: false,
            select_word: '',
            upForm: {
                id: 0,
                name: '',
                note: '',
            },
            addForm: {
                name: '',
                note: '',
            },
            formLabelWidth: '120px'
        }
    },
    created() {
        this.getRoles()
        console.log(this.tableData)
    },
    computed: mapState({
        page: state => state.Role.roles,
        tableData: state => state.Role.roles.content,
        user: state => state.Login.user
    }),
    methods: {
        ...mapActions(['saveRole', 'add_role', 'del_role']),
        getRoles() {
            allRoles()
                .then(res => {
                    if (res.status === 0) {
                        this.saveRole(res)
                    }
                })
        },
        handleClick(row) {
            console.log(row)
        },
        formatRole(row, col) {
            let roles = this.roles
            for (let i in roles) {
                if (roles[i].id === row.roleId) {
                    return roles[i].name
                }
            }
            return '未知'
        },
        search() {
            let index = this.page.number + 1
            this.handleCurrentChange(index)
        },
        handleCurrentChange(val) {
            console.log(`当前页: ${val}`);
            let field = 'name'
            let keyword = this.select_word
            let index = val
            let size = this.page.size
            allRoles({ field, keyword, index, size })
                .then(res => {
                    if (res.status === 0) {
                        this.saveRole(res)
                    } else {
                        this.$message.error(res.message)
                    }
                }, err => this.$message.error('网络异常'))
        },
        delRole(row) {
            this.$confirm('此操作将永久删除该角色, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                let userId = this.user.id
                let id = row.id
                delRole({ id, userId })
                    .then(res => {
                        if (res.status === 0) {
                            console.log(res)
                            this.del_role(id)
                            this.$message({
                                type: 'success',
                                message: '删除成功!'
                            });
                        }
                    })
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消删除'
                });
            });

        },
        add() {
            console.log(this.addForm)
            let roleEntity = this.addForm
            let userId = this.user.id
            // roleEntity.userId = this.user.id
            addRole({ roleEntity, userId })
                .then(res => {
                    if (res.status === 0) {
                        this.dialogFormVisible = false
                        this.$message.info("添加成功")
                        this.add_role(res)
                        this.addForm = {}
                    } else {
                        this.$message.error(res.message)
                    }
                }, err => this.$message.error("网络异常"))
        },
        edit(row) {
            this.dialogFormUpdate = true
            this.upForm.name = row.name
            this.upForm.note = row.note
            this.upForm.id = row.id
        },
        putRole() {
            let roleEntity = this.upForm
            let userId = this.user.id
            putRole({ roleEntity, userId })
                .then(res => {
                    if (res.status === 0) {
                        this.dialogFormUpdate = false
                        this.updateRole(res)
                    } else {
                        this.$message.error(res.message)
                    }
                }, err => this.$message.error(res.message))
        }
    }
}
</script>
<style lang="scss" scoped>
.handle-box {
  margin-bottom: 1%;
  .search {
    width: 30%;
  }
}

.block {
  text-align: center;
  margin-top: 1%;
}
</style>